package com.lg.mengbase.testBaseRecyclerAdapter.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;

import com.lg.baselibrary.activity.BaseToolbarActivity;
import com.lg.baselibrary.adapter.BaseRecyclerViewAdapter;
import com.lg.baselibrary.adapter.BaseViewHolder;
import com.lg.baselibrary.widget.BaseRecyclerView;
import com.lg.mengbase.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LiGang on 2017/5/27.
 */

public class TestMoreLayoutRecyclerViewActivity extends BaseToolbarActivity {
    @BindView(R.id.recyclerView)
    BaseRecyclerView recyclerView;
    private MyAdapter adapter;
    @Override
    protected int provideContentViewLayout() {
        return R.layout.activity_baserecyclerviewadapter;
    }
    @Override
    protected void initTitle() {
        setToolbarTitle("BaseRecyclerViewAdapter");
    }
    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        recyclerView.setLinearLayoutManager(LinearLayoutManager.VERTICAL);
        adapter = new MyAdapter(this);
        adapter.addDatas(getDatas());
        recyclerView.setAdapter(adapter);
    }

    private List<ItemMsg> getDatas() {
        List<ItemMsg> datas = new ArrayList<>();
        datas.add(new ItemMsg("你好啊", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("在不在", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("在，有什么事？", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("木什么事啊", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("无聊啊", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("来数数啊", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("......", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("好不好伐", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("好吧，你先", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("1", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("2", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("3", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("4", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("5", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("6", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("7", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("8", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("9", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("10", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("继续啊", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("不会睡着了吧", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("......", ItemMsg.MessageDirection.SEND));
        return datas;
    }

    static class ItemMsg{
        String content;
        MessageDirection direction;

        public ItemMsg(String content, MessageDirection direction) {
            this.content = content;
            this.direction = direction;
        }

        public static enum MessageDirection {
            SEND(1),
            RECEIVE(2);
            private int value = 1;
            private MessageDirection(int value) {
                this.value = value;
            }
        }
    }
    class MyAdapter extends BaseRecyclerViewAdapter<ItemMsg>{
        private static final int LAYOUT_RECEIVER = R.layout.item_session_text_left;
        private static final int LAYOUT_SEND = R.layout.item_session_text_right;
        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        protected int getItemViewLayout(int position) {
            ItemMsg itemMsg = getItem(position);
            if(itemMsg.direction == ItemMsg.MessageDirection.RECEIVE){
                return LAYOUT_RECEIVER;
            }
            if(itemMsg.direction == ItemMsg.MessageDirection.SEND){
                return LAYOUT_SEND;
            }
            return 0;
        }

        @Override
        public void convert(BaseViewHolder viewHolder, ItemMsg item, int position) {
            if(getItemViewLayout(position) == LAYOUT_RECEIVER){
                viewHolder.setText(R.id.tv_msg_receive,item.content);
            }else{
                viewHolder.setText(R.id.tv_msg_send,item.content);
            }
        }
    }
}
